package com.doctor.windflower_doctor.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.adapter.InspectionListAdapter;
import com.doctor.windflower_doctor.entity.InspectionBeen;
import com.doctor.windflower_doctor.myuser.tools.HttpclientHelper;
import com.doctor.windflower_doctor.util.Constent;

public class InspectionActivity extends BaseActivity implements Constent{
private ListView listView;
private ImageButton back;
private List<InspectionBeen>list;
private InspectionListAdapter adapter;
	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		list=new ArrayList<InspectionBeen>();
		back=(ImageButton) findViewById(R.id.imageButton_back);
		listView=(ListView) findViewById(R.id.listView1);
		adapter=new InspectionListAdapter(InspectionActivity.this);
		new InspectionTask().execute(RECORD_URL_ONLINE+CHECKLIST+SystemApplication.getInstance().doctorId);
		
	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.inspection;
	}
	class InspectionTask extends AsyncTask<String, Void, String>{

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return HttpclientHelper.getAddress(params[0]);
		}
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(result!=null){
				
				list=json(result);
				adapter.addListData(list);
				listView.setAdapter(adapter);
			}else{
				Toast.makeText(InspectionActivity.this, "请检查网络状态", Toast.LENGTH_SHORT).show();
			}
		}
	}

	
		     
    public List<InspectionBeen> json(String json){
  	  JSONObject jsonObject;
  	List<InspectionBeen>list=new ArrayList<InspectionBeen>();
	try {
		jsonObject = new JSONObject(json);
	
  	
  	
		if(jsonObject.getBoolean("success")){
			
			JSONObject jsonObject2=jsonObject.getJSONObject("data");
			JSONArray jsonArray=jsonObject2.getJSONArray("lists");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject3=jsonArray.getJSONObject(i);
				InspectionBeen been=new InspectionBeen();
				
				been.setCreatedAt(jsonObject3.getString("createdAt"));
				been.setPhotos(jsonObject3.getString("photos"));
				list.add(been);
				
				
			}
		}
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return list;
    }
}
